import {
  Component,
  OnInit,
  ComponentFactoryResolver,
  ViewChild,
  ViewContainerRef
} from "@angular/core";
import { ComponentType } from "@angular/cdk/portal";
import { BaseInfoComponent } from "../base-info/base-info.component";
import { MonitorComponent } from "../monitor/monitor.component";
import { NzMessageService } from "ng-zorro-antd";
import { MapUiService } from '../map-ui.service';
import { ZTList } from '../one-map';
const ZTMapper: any = {
  'base': BaseInfoComponent,
  'monitor': MonitorComponent
};

@Component({
  selector: "app-zhuanti",
  templateUrl: "./zhuanti.component.html",
  styleUrls: ["./zhuanti.component.scss"]
})
export class ZhuantiComponent implements OnInit {
  @ViewChild("content", { read: ViewContainerRef, static: true }) contentRef;
  private deployUrl = (window as any).environment.deployUrl;

  constructor(
    private componentFactoryResolver: ComponentFactoryResolver,
    private message: NzMessageService,
    private mapUiService: MapUiService
  ) { }

  ngOnInit(): void {
    this.onActiveZT(0);
    this.mapUiService.ztChange.subscribe((idx) => {
      this.onActiveZT(idx);
    });
  }

  onActiveZT(idx) {
    if (ZTList[idx].comp) {
      this.loadComponent(ZTMapper[ZTList[idx].comp], {});
    } else if (ZTList[idx].url) {
      window.open(`${this.deployUrl}${ZTList[idx].url}`);
    } else {
      this.message.warning("正在开发中");
    }
  }

  loadComponent<T>(contentComp: ComponentType<T>, data: any): void {
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(
      contentComp
    );
    this.contentRef.clear();
    const contentComponentRef = this.contentRef.createComponent(
      componentFactory
    );
    contentComponentRef.instance.data = data;
  }
}
